package configs

import (
	"github.com/apulis/sdk/go-utils/logging"
	"github.com/spf13/viper"
)

var (
	Config AppConfig
)

type AppConfig struct {
	Port               string          `mapstructure:"port"`
	Prefix             string          `mapstructure:"prefix"`
	MaxSize            int64           `mapstructure:"maxSize"`
	StoreType          string          `mapstructure:"storeType"`
	FileStore          FileStoreConfig `mapstructure:"file"`
	S3Store            S3StoreConfig   `mapstructure:"s3"`
	PreCreateHooks     []Hooks         `mapstructure:"preCreate"`
	PreFinishHooks     []Hooks         `mapstructure:"preFinish"`
	PostCreateHooks    []Hooks         `mapstructure:"postCreate"`
	PostReceiveHooks   []Hooks         `mapstructure:"postReceive"`
	PostFinishHooks    []Hooks         `mapstructure:"postFinish"`
	PostTerminateHooks []Hooks         `mapstructure:"postTerminate"`
}

type FileStoreConfig struct {
	PvcRootPath string `mapstructure:"pvc"`
	MountPath   string `mapstructure:"mountPath"`
}

type S3StoreConfig struct {
}

type Hooks struct {
	ModuleName string `mapstructure:"moduleName"`
	Endpoint   string `mapstructure:"endpoint"`
}

// Init 配置初始化
func Init() {
	viper.SetConfigName("config")
	viper.AddConfigPath(".")
	viper.SetConfigType("yaml")
	viper.AutomaticEnv()
	viper.AllowEmptyEnv(true)

	if err := viper.ReadInConfig(); err != nil {
		logging.Fatal().Err(err).Msg("config: viper reading config file failed")
	}

	err := viper.Unmarshal(&Config)
	if err != nil {
		logging.Fatal().Err(err).Msg("config: viper decode config failed")
	}
}
